from ctypes import *

core = cdll.LoadLibrary('./Live2DCubismCore.dll')
data = open('./chuixue_3/chuixue_3.moc3', 'rb').read()

LOGFUNC = CFUNCTYPE(None, c_char_p)
core.csmSetLogFunction.argtypes = [LOGFUNC]
core.csmSetLogFunction(LOGFUNC(lambda x: print(x)))

buf = (c_ubyte * len(data)).from_buffer_copy(data)
# print(buf)
core.csmReviveMocInPlace.restype = c_void_p
moc = core.csmReviveMocInPlace(buf, len(data))

# print(hex(moc))
core.csmGetSizeofModel.argtypes = [c_void_p]
model_size = core.csmGetSizeofModel(moc)
print('model size', hex(model_size))

model_buf = create_string_buffer(model_size)
core.csmInitializeModelInPlace.argtypes = [c_void_p, c_void_p, c_int]
core.csmInitializeModelInPlace.restype = c_void_p
model = core.csmInitializeModelInPlace(moc, model_buf, model_size)

core.csmGetDrawableCount.argtypes = [c_void_p]
drawable_cnt = core.csmGetDrawableCount(model)
print('count', drawable_cnt)

core.csmGetDrawableVertexUvs.argtypes = [c_void_p]
core.csmGetDrawableVertexUvs.restype = POINTER(POINTER(c_int64))
uvs = core.csmGetDrawableVertexUvs(model)

core.csmGetDrawableVertexCounts.argtypes = [c_void_p]
core.csmGetDrawableVertexCounts.restype = POINTER(c_int32)
vertex_counts = core.csmGetDrawableVertexCounts(model)

core.csmGetDrawableVertexPositions.argtypes = [c_void_p]
core.csmGetDrawableVertexPositions.restype = POINTER(POINTER(c_uint64))
poss = core.csmGetDrawableVertexPositions(model)

# o_buf = create_string_buffer(1024)
# memmove(o_buf, uvs.contents, 8)

with open('temp', 'wb') as fp:
    for i in range(drawable_cnt):
        for j in range(vertex_counts[i]):
            fp.write(int.to_bytes(uvs[i][j], 8, 'little'))

with open('temp_pos', 'wb') as fp:
    for i in range(drawable_cnt):
        for j in range(vertex_counts[i]):
            fp.write(int.to_bytes(poss[i][j], 8, 'little'))
